/*
 * Copyright 2009 Thor Hovden
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the
 * License. 
 */
package eviva.application;

import java.util.ArrayList;

import eviva.exceptions.FieldMismatchException;

public class Concept {

	String name;
	ArrayList<String> fields = new ArrayList<String>();
	ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
	
	public Concept(String name) {
		this.name = name;
	}

	public void addFields(ArrayList<String> list) {
		fields.addAll(list);
	}

	/**
	 * Add a data record to this concept
	 * @param list list of field values, must match fields, or a FieldMismatchException will be thrown
	 * @throws FieldMismatchException 
	 */
	public void addDataRecord(ArrayList<String> list) throws FieldMismatchException {
		if(list.size() != fields.size()) {
			throw new FieldMismatchException("Concept "+name+" is not accepting data not matching its fields");
		}
		data.add(list);
	}

	public ArrayList<String> getDataRecord(int index) {
		return data.get(index);
	}

	public String listFields() {
		StringBuffer b = new StringBuffer(name);
		b.append("= {");
		for (int i = 0; i < fields.size(); i++) {
			b.append(fields.get(i));
			if (i < (fields.size() - 1)) {
				b.append(", ");
			}
		}
		b.append("}");
		return b.toString();
	}
	
	public String listFieldsWithData() {
		StringBuffer b = new StringBuffer(name);
		b.append("= {");
		for (int i = 0; i < fields.size(); i++) {
			b.append(fields.get(i));
			b.append("=\"");
			b.append(data.get(i));
			b.append("\"");
			if (i < (fields.size() - 1)) {
				b.append(", ");
			}
		}
		b.append("}");
		return b.toString();
	}
}
